//
//  ShoppingC.m
//  ShopDemo
//
//  Created by 飞扬的青春 on 2018/7/3.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "ShoppingC.h"
#import "ThirdViewCell.h"
#import "AddressCon.h"
#import "LoginController.h"
@interface ShoppingC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *listTableview;
@property(nonatomic,strong)NSMutableArray *datasource;
@property(nonatomic,strong)NSMutableArray *selectArray;
@property(nonatomic,strong)NSMutableArray *selectSouceArray;
@property(nonatomic,strong)UIView *bottomview;
@property(nonatomic,assign)CGFloat shopsum;
@property(nonatomic,strong)UILabel *pricelab;
@property(nonatomic,strong)MineButton *allbtn;
@property(nonatomic,strong)NSMutableDictionary *sumMutabdic;

@property(nonatomic,assign)NSInteger index;
@property(nonatomic,assign)NSInteger page;


@end

@implementation ShoppingC
-(NSMutableArray *)datasource{
    if (!_datasource) {
        _datasource = [NSMutableArray arrayWithCapacity:10];
    }
    return _datasource;
}
-(NSMutableDictionary *)sumMutabdic{
    if (!_sumMutabdic) {
        _sumMutabdic = [NSMutableDictionary dictionaryWithCapacity:10];
    }
    return _sumMutabdic;
}
-(NSMutableArray *)selectArray{
    if (!_selectArray) {
        _selectArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _selectArray;
}

-(NSMutableArray *)selectSouceArray{
    if (!_selectSouceArray) {
        _selectSouceArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _selectSouceArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   NSString *username = [UserDefault objectForKey:LogUserName];
    if (username && username.length > 0) {
        if (!self.listTableview) {
            [self creatTableviewMethod];
        }
        self.index = 1;
        self.page = 0;
        self.datasource = [NSMutableArray array];
        NSString *strurl = [NSString stringWithFormat:@"/dashop/shoppingcar/app/list.do?USERNAME=%@&CurrentPage=%ld",[UserDefault objectForKey:LogUserID],(long)self.index];
        [self showDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:strurl]];
    }else{
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"去登陆" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(goLoginButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:LineColor forState:UIControlStateNormal];
        [self.view addSubview:btn];
        btn.sd_layout
        .centerXEqualToView(self.view)
        .topSpaceToView(self.view, 10*kJLXWidthScale)
        .heightIs(40*kJLXWidthScale)
        .widthIs(80*kJLXWidthScale);
    }
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.sumMutabdic.count > 0) {
        for (NSString *shopidstr in [self.sumMutabdic allKeys]) {
            for (NSDictionary *dic in self.datasource) {
                if ([dic[@"SHOPPINGCAR_ID"]isEqualToString:shopidstr]) {
                    NSString *strurl = [NSString stringWithFormat:@"/dashop/shoppingcar/app/update.do?SHOPPINGCAR_ID=%@&USERNAME=%@&GOODS_NUM=%@",dic[@"SHOPPINGCAR_ID"],[UserDefault objectForKey:LogUserID],self.sumMutabdic[shopidstr]];
                    [self shopCarNumMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:strurl]];
                }
            }
        }
    }
}


-(void)shopCarNumMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            if ([dic[@"RESULT"] integerValue] == 1) {
                NSString *strurl = [NSString stringWithFormat:@"/dashop/shoppingcar/app/list.do?USERNAME=%@&CurrentPage=1",[UserDefault objectForKey:LogUserID]];
                [self showDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:strurl]];
            }
        }];
    } failBlock:^(NSError *error) {
        
    }];
}


//添加清空按钮
-(void)addSettingButtonMethod{
    UIBarButtonItem *btn = [[UIBarButtonItem  alloc]initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(settingButtonAction:)];
    self.navigationItem.rightBarButtonItem = btn;
}
-(void)settingButtonAction:(UIBarButtonItem *)item{
    
    NSString *urlstr = [NSString stringWithFormat:@"/dashop/shoppingcar/app/delete.do?USERNAME=%@",[UserDefault objectForKey:LogUserID]];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:urlstr];
    [self deleShopCarMethodWithUrl:endstr];
    
}





-(void)deleShopCarMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        KMLog(@"%@",response);
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            if ([dic[@"RESULT"] integerValue] == 1) {
                NSString *strurl = [NSString stringWithFormat:@"/dashop/shoppingcar/app/list.do?USERNAME=%@&CurrentPage=1",[UserDefault objectForKey:LogUserID]];
                self.datasource = [NSMutableArray array];
                [self showDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:strurl]];
                self.shopsum = 0.0;
                self.allbtn.mineimagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
                NSString *str = [NSString stringWithFormat:@"%.2f",self.shopsum];
                NSString*string =[NSString stringWithFormat:@"合计支付：%@",str];
                NSRange range = [string rangeOfString: str];
                NSMutableAttributedString*attribute = [[NSMutableAttributedString alloc] initWithString: string];
                [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor orangeColor]}range: range];
                [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor redColor]}range: NSMakeRange(0, range.location)];
                [self.pricelab setText: string];
                [self.pricelab setAttributedText: attribute];
            }
        }];
    } failBlock:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.edgesForExtendedLayout = UIRectEdgeNone;
    [self addSettingButtonMethod];
    self.shopsum = 0;
    [self creatBottomviewMethod];
    self.listTableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.page == 10 ) {
            self.index ++;
            NSString *strurl = [NSString stringWithFormat:@"/dashop/shoppingcar/app/list.do?USERNAME=%@&CurrentPage=%ld",[UserDefault objectForKey:LogUserID],(long)self.index];
            [self showDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:strurl]];

        }else{
            [self.listTableview.mj_footer endRefreshingWithNoMoreData];
        }
    }];
    
   
}

-(void)goLoginButtonAction:(UIButton *)sender{
    LoginController *loginVC = [[LoginController alloc]init];
    loginVC.logBlock = ^(NSDictionary *logdic) {
        
    };
    loginVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:loginVC animated:YES];
}

-(void)showDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSArray *arr = response;
        self.page = arr.count;
        for (NSDictionary *dic in arr) {
            NSMutableDictionary *mudic = [dic mutableCopy];
            if (self.sumMutabdic.count > 0) {
                if ([[self.sumMutabdic allKeys]containsObject:dic[@"SHOPPINGCAR_ID"]]) {
                    NSString *keystr = self.sumMutabdic[dic[@"SHOPPINGCAR_ID"]];
                    [mudic setValue:keystr forKey:@"GOODS_NUM"];
                }
            }
            [self.datasource addObject:mudic];
        }
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.listTableview reloadData];
        }];
    } failBlock:^(NSError *error) {
        
    }];
}
-(void)creatTableviewMethod{
    self.listTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 180*kJLXWidthScale) style:UITableViewStylePlain];
    [self.view addSubview:self.listTableview];
    if (kScreenHeight == 812){
        self.listTableview.sd_layout
        .bottomSpaceToView(self.view, 68);
    }
    self.listTableview.dataSource = self;
    self.listTableview.delegate = self;
    [self.listTableview registerClass:[ThirdViewCell class] forCellReuseIdentifier:@"onecell"];
    //分隔线
    self.listTableview.separatorStyle = UITableViewCellAccessoryNone;
    //去掉尾部多余的空白
    [self.listTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
    
}

-(void)creatBottomviewMethod{
    UIView *bottomview = [UIView new];
    self.bottomview = bottomview;
    bottomview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomview];
    bottomview.sd_layout
    .rightSpaceToView(self.view, 0)
    .leftSpaceToView(self.view, 0)
    .heightIs(51*kJLXWidthScale)
    .bottomSpaceToView(self.view, 0);

    UILabel *linelab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    linelab.backgroundColor = [UIColor orangeColor];
    [bottomview addSubview:linelab];
    
    
    MineButton *allseleBtn = [MineButton buttonWithType:UIButtonTypeCustom];
    self.allbtn = allseleBtn;
    [bottomview addSubview:allseleBtn];
    allseleBtn.sd_layout
    .centerYEqualToView(bottomview)
    .leftSpaceToView(bottomview, 8*kJLXWidthScale)
    .heightIs(30*kJLXWidthScale)
    .widthIs(60*kJLXWidthScale);
    
    allseleBtn.mineimagevi = [UIImageView new];
    allseleBtn.mineimagevi.userInteractionEnabled = NO;
   allseleBtn.mineimagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
    [allseleBtn addSubview:allseleBtn.mineimagevi];
    allseleBtn.mineimagevi.sd_layout
    .centerYEqualToView(allseleBtn)
    .leftSpaceToView(allseleBtn, 10*kJLXWidthScale)
    .widthIs(15*kJLXWidthScale)
    .heightIs(15*kJLXWidthScale);
    
    allseleBtn.minlab = [UILabel new];
    allseleBtn.minlab.userInteractionEnabled = NO;
    allseleBtn.minlab.text = @"全选";
    allseleBtn.minlab.textAlignment = NSTextAlignmentLeft;
    [allseleBtn addSubview:allseleBtn.minlab];
    allseleBtn.minlab.sd_layout
    .centerYEqualToView(allseleBtn)
    .leftSpaceToView(allseleBtn.mineimagevi, 5*kJLXWidthScale)
    .heightIs(30*kJLXWidthScale)
    .widthIs(40*kJLXWidthScale);
    
    [allseleBtn addTarget:self action:@selector(allseleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *sumBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    sumBtn.backgroundColor = [UIColor redColor];
    sumBtn.titleLabel.font = [UIFont systemFontOfSize:20*kJLXWidthScale];
    [sumBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sumBtn setTitle:@"支付" forState:UIControlStateNormal];
    [sumBtn addTarget:self action:@selector(sumButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomview addSubview:sumBtn];
    sumBtn.sd_layout
    .topSpaceToView(bottomview, 0)
    .rightSpaceToView(bottomview, 0)
    .bottomSpaceToView(bottomview, 0)
    .widthIs(80*kJLXWidthScale);
    
    
    UILabel *pricelab = [UILabel new];
    self.pricelab = pricelab;
    pricelab.numberOfLines = 1;
    pricelab.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
    pricelab.textColor = [UIColor orangeColor];
    pricelab.textAlignment = NSTextAlignmentRight;
    [bottomview addSubview:pricelab];
    pricelab.sd_layout
    .centerYEqualToView(bottomview)
    .rightSpaceToView(sumBtn, 5*kJLXWidthScale)
    .heightIs(30*kJLXWidthScale)
    .leftSpaceToView(allseleBtn, 5*kJLXWidthScale);
    NSString *str = [NSString stringWithFormat:@"%.2f",self.shopsum];
    NSString*string =[NSString stringWithFormat:@"合计支付：%@",str];
    NSRange range = [string rangeOfString: str];
    NSMutableAttributedString*attribute = [[NSMutableAttributedString alloc] initWithString: string];
    [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor orangeColor]}range: range];
    [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor redColor]}range: NSMakeRange(0, range.location)];
    [self.pricelab setText: string];
    [self.pricelab setAttributedText: attribute];
}

-(void)allseleButtonAction:(MineButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        CGFloat shopNum = 0;
        for (int i = 0; i < self.datasource.count ; i ++) {
            NSMutableDictionary *shopdic = self.datasource[i] ;
            if (self.sumMutabdic.count > 0) {
                if ([[self.sumMutabdic allKeys]containsObject:shopdic[@"SHOPPINGCAR_ID"]]) {
                    NSString *keystr = self.sumMutabdic[shopdic[@"SHOPPINGCAR_ID"]];
                    shopNum = [keystr integerValue]*[shopdic[@"GOODS_PRICE"] floatValue] + shopNum;
                    [shopdic setValue:keystr forKey:@"GOODS_NUM"];
                }else{
                  shopNum = [shopdic[@"GOODS_NUM"] integerValue]*[shopdic[@"GOODS_PRICE"] floatValue] + shopNum;
                }
            }else{
                shopNum = [shopdic[@"GOODS_NUM"] integerValue]*[shopdic[@"GOODS_PRICE"] floatValue] + shopNum;
            }
        }
        self.shopsum = shopNum;
        NSString *str = [NSString stringWithFormat:@"%.2f",self.shopsum];
        NSString*string =[NSString stringWithFormat:@"合计支付：%@",str];
        NSRange range = [string rangeOfString: str];
        NSMutableAttributedString*attribute = [[NSMutableAttributedString alloc] initWithString: string];
        [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor orangeColor]}range: range];
        [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor redColor]}range: NSMakeRange(0, range.location)];
        [self.pricelab setText: string];
        [self.pricelab setAttributedText: attribute];
        sender.mineimagevi.image = [UIImage imageNamed:@"LoginSelect"];
        self.selectSouceArray = [NSMutableArray arrayWithCapacity:10];
        for (int i = 0; i < self.datasource.count; i ++) {
            NSDictionary *dic = self.datasource[i];
            [self.selectSouceArray addObject:dic];
            NSString *rowstr = [NSString stringWithFormat:@"%d",i];
            if (![self.selectArray containsObject:rowstr]) {
                [self.selectArray addObject:rowstr];
            }
        }
        [self.listTableview reloadData];
    }else{
        self.shopsum = 0;
        NSString *str = [NSString stringWithFormat:@"%.2f",self.shopsum];
        NSString*string =[NSString stringWithFormat:@"合计支付：%@",str];
        NSRange range = [string rangeOfString: str];
        NSMutableAttributedString*attribute = [[NSMutableAttributedString alloc] initWithString: string];
        [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor orangeColor]}range: range];
        [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor redColor]}range: NSMakeRange(0, range.location)];
        [self.pricelab setText: string];
        [self.pricelab setAttributedText: attribute];
        sender.mineimagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
        [self.selectArray removeAllObjects];
        [self.listTableview reloadData];
    }
    
    
    
    
}

-(void)sumButtonAction:(UIButton *)sender{
    NSString *username = [UserDefault objectForKey:LogUserName];
    if (username && username.length > 0) {
        if (self.selectSouceArray.count > 0) {
            NSMutableString *str1 = [NSMutableString stringWithCapacity:10];
            NSMutableString *str2 = [NSMutableString stringWithCapacity:10];
            NSMutableString *str3 = [NSMutableString stringWithCapacity:10];
            for (NSDictionary *dic in self.selectSouceArray) {
                [str1 appendFormat:@"%@,",dic[@"GOODS_ID"]];
                [str2 appendFormat:@"%@,",dic[@"GOODS_ATTRIBUTE_ID"]];
                if (_sumMutabdic.count > 0 && [[_sumMutabdic allKeys] containsObject:dic[@"SHOPPINGCAR_ID"]] ) {
                    [str3 appendFormat:@"%@,",_sumMutabdic[dic[@"SHOPPINGCAR_ID"]]];
                }else{
                    [str3 appendFormat:@"%@,",dic[@"GOODS_NUM"]];
                }
            }
            AddressCon *addVC = [[AddressCon alloc]init];
            addVC.sumPriceStr = self.pricelab.text;
            addVC.seleSCarArray = self.selectSouceArray;
            addVC.shopId = [str1 substringToIndex:str1.length -1];
            addVC.shopsxId = [str2 substringToIndex:str2.length -1];
            addVC.shopNumId = [str3 substringToIndex:str3.length -1];
            addVC.hidesBottomBarWhenPushed = YES;
            __weak typeof(self)weakself = self;
            addVC.successBlock = ^{
                weakself.selectSouceArray = [NSMutableArray array];
                weakself.pricelab.text = @"0.00";
                weakself.selectArray = [NSMutableArray array];
                weakself.sumMutabdic = [NSMutableDictionary dictionary];
                [self.listTableview reloadData];
            };
            /*
             接口传参
             GOODS_ID 商品ID（必须）
             ATTRIBUTE_DETAIL_ID 商品属性ID（必须）
             GOODS_COUNT=1 数量（必须）
             USER_ID 用户ID （必须）
             COUPON_ID 优惠券ID（不是必须）
             否需要分页
             否
             接口地址
             order/app/order_total.do
             返回接口
             map
             选择商品到确认订单界面（可以直接选商品-立即购买，或者在购物车选多个商品--确认订单界面）
             多个商品时 GOODS_ID、ATTRIBUTE_DETAIL_ID、GOODS_COUNT  中间用“,” 隔开
             */
            NSString *strurl = [NSString stringWithFormat:@"/dashop/order/app/order_total.do?GOODS_ID=%@&ATTRIBUTE_DETAIL_ID=%@&GOODS_COUNT=%@&USER_ID=%@&COUPON_ID=",str1,str2,str3,[UserDefault objectForKey:LogUserID]];
            NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
            [self querenOrderMethodWithUrl:endstr];
            [self.navigationController pushViewController:addVC animated:YES];
        }
    }else{
        LoginController *loginVC = [[LoginController alloc]init];
        loginVC.logBlock = ^(NSDictionary *logdic) {
            
        };
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }
   
    
}

-(void)querenOrderMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        
       
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            
        }];
    } failBlock:^(NSError *error) {
        
    }];
}



#pragma mark --UITableViewDelegate,UITableViewDataSource--


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    ThirdViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//cell选中以后的颜色
    if (self.datasource.count > 0) {
        NSDictionary *dic = self.datasource[indexPath.row];
        NSString *urlstr = [MethodClass returnUrlSetStrWSDLURL:dic[@"GOODS_PIC"]];
        NSURL *url = [NSURL URLWithString:urlstr];
        [cell.titimagevi sd_setImageWithURL:url];
        cell.titlab.text = [NSString stringWithFormat:@"%@",dic[@"GOODS_NAME"]];
        cell.scelab.text = [NSString stringWithFormat:@"%@",dic[@"GOODS_ATTRIBUTE"]];
        cell.pricelab.text = [NSString stringWithFormat:@"￥%@",dic[@"GOODS_PRICE"]];
        cell.typeImagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
        [cell creatShopBtnNum:[dic[@"GOODS_NUM"] integerValue]];
        cell.delebtn.usinfo = dic;
        cell.delebtn.tag = 330 + indexPath.row;
        [cell.delebtn addTarget:self action:@selector(deleshopAction:) forControlEvents:UIControlEventTouchUpInside];
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        }
        
        NSString *rowStr = [NSString stringWithFormat:@"%ld",indexPath.row];
        if (self.selectArray.count > 0) {
            if ([self.selectArray containsObject:rowStr]) {
                cell.typeImagevi.image = [UIImage imageNamed:@"LoginSelect"];
            }
        }
        
        cell.shopNumBlock = ^(NSInteger num) {
            
            [self.sumMutabdic setValue:[NSString stringWithFormat:@"%ld",num] forKey:dic[@"SHOPPINGCAR_ID"]];
            self.shopsum = 0;
            for (NSDictionary *subdic in self.selectSouceArray) {
                if (self.sumMutabdic.count > 0) {
                    if ([[self.sumMutabdic allKeys]containsObject:subdic[@"SHOPPINGCAR_ID"]]) {
                        NSString *key = subdic[@"SHOPPINGCAR_ID"];
                        self.shopsum = self.shopsum + [self.sumMutabdic[key] integerValue]*[subdic[@"GOODS_PRICE"]floatValue];
                    }else{
                        self.shopsum = self.shopsum + [subdic[@"GOODS_NUM"]integerValue]*[subdic[@"GOODS_PRICE"]floatValue];
                    }
                }else{
                    self.shopsum = self.shopsum + [subdic[@"GOODS_NUM"]integerValue]*[subdic[@"GOODS_PRICE"]floatValue];
                }
            }
            NSString *str = [NSString stringWithFormat:@"%.2f",self.shopsum];
            NSString*string =[NSString stringWithFormat:@"合计支付：%@",str];
            NSRange range = [string rangeOfString: str];
            NSMutableAttributedString*attribute = [[NSMutableAttributedString alloc] initWithString: string];
            [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor orangeColor]}range: range];
            [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor redColor]}range: NSMakeRange(0, range.location)];
            [self.pricelab setText: string];
            [self.pricelab setAttributedText: attribute];
        };
    }
    return cell;
    
}
//单个删除
-(void)deleshopAction:(MineButton *)sender{
    //先发送修改后的数量再删除
    if (self.sumMutabdic.count > 0) {
        for (NSString *shopidstr in [self.sumMutabdic allKeys]) {
            for (NSDictionary *dic in self.datasource) {
                if ([dic[@"SHOPPINGCAR_ID"]isEqualToString:shopidstr]) {
                    NSString *strurl = [NSString stringWithFormat:@"/dashop/shoppingcar/app/update.do?SHOPPINGCAR_ID=%@&USERNAME=%@&GOODS_NUM=%@",dic[@"SHOPPINGCAR_ID"],[UserDefault objectForKey:LogUserID],self.sumMutabdic[shopidstr]];
                    [self shopCarNumMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:strurl]];
                }
            }
        }
    }
    [self.selectArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj integerValue] == sender.tag - 330) {
            [self.selectArray removeObject:obj];
        }
    }];
    NSString *urlstr = [NSString stringWithFormat:@"/dashop/shoppingcar/app/delete.do?SHOPPINGCAR_ID=%@",sender.usinfo[@"SHOPPINGCAR_ID"]];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:urlstr];
    [self deleShopCarMethodWithUrl:endstr];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 135*kJLXWidthScale;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *rowStr = [NSString stringWithFormat:@"%ld",indexPath.row];
    ThirdViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];//获取cell
    if (self.selectArray.count > 0) {
        if ([self.selectArray containsObject:rowStr]) {
            cell.typeImagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
            NSMutableArray *muarr = [NSMutableArray arrayWithCapacity:10];
            for (NSString *rowstr in self.selectArray) {
                NSInteger num = [rowstr integerValue];
                if (num == indexPath.row) {
                    
                }else{
                    [muarr addObject:rowstr];
                }
            }
            self.selectArray = muarr;
            self.allbtn.mineimagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
        }else{
            [self.selectArray addObject:rowStr];
            cell.typeImagevi.image = [UIImage imageNamed:@"LoginSelect"];
        }
        if (self.selectArray.count == self.datasource.count) {
            self.allbtn.mineimagevi.image = [UIImage imageNamed:@"LoginSelect"];
        }
    }else{
        [self.selectArray addObject:rowStr];
        cell.typeImagevi.image = [UIImage imageNamed:@"LoginSelect"];
    }
    NSMutableArray *muarr = [NSMutableArray arrayWithCapacity:10];
    self.shopsum = 0;
    for (NSString *numstr in self.selectArray) {
        NSInteger num = [numstr integerValue];
        NSDictionary *dic = self.datasource[num];
        [muarr addObject:dic];
        if (self.sumMutabdic.count > 0) {
            if ([[self.sumMutabdic allKeys]containsObject:dic[@"SHOPPINGCAR_ID"]]) {
                NSString *key = dic[@"SHOPPINGCAR_ID"];
                self.shopsum = self.shopsum + [self.sumMutabdic[key] integerValue]*[dic[@"GOODS_PRICE"]floatValue];
            }else{
                self.shopsum = self.shopsum + [dic[@"GOODS_NUM"]integerValue]*[dic[@"GOODS_PRICE"]floatValue];
            }
        }else{
            self.shopsum = self.shopsum + [dic[@"GOODS_NUM"]integerValue]*[dic[@"GOODS_PRICE"]floatValue];
        }
        
    }
    
    NSString *str = [NSString stringWithFormat:@"%.2f",self.shopsum];
    NSString*string =[NSString stringWithFormat:@"合计支付：%@",str];
    NSRange range = [string rangeOfString: str];
    NSMutableAttributedString*attribute = [[NSMutableAttributedString alloc] initWithString: string];
    [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor orangeColor]}range: range];
    [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor redColor]}range: NSMakeRange(0, range.location)];
    [self.pricelab setText: string];
    [self.pricelab setAttributedText: attribute];
    
    self.selectSouceArray = muarr;
    
    
    
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *rowStr = [NSString stringWithFormat:@"%ld",indexPath.row];
    if (self.selectArray.count > 0) {
        if ([self.selectArray containsObject:rowStr]) {
            ThirdViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];//获取cell
            cell.typeImagevi.image = [UIImage imageNamed:@"LoginSelect"];
        }{
            
        }
    }
    
    
    
    
}

@end
